home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10532 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  69 lines

  1. Path: news.atw.fullfeed.com!usenet
  2. From: lam@adci.com (Lori A. MacVittie)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie question re: struct pointers & member access
  5. Date: 8 Mar 1996 14:56:58 GMT
  6. Organization: ADCI
  7. Message-ID: <4hphrr$4s0@ray.atw.fullfeed.com>
  8. References: <4hn0v2$7q5@aahz.magic.mb.ca>
  9. NNTP-Posting-Host: 204.145.195.173
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.6
  13.  
  14. In article <4hn0v2$7q5@aahz.magic.mb.ca>, jranta@astral.magic.ca@ 
  15. says...
  16.  
  17. >void enter(int t)
  18. >{
  19. > car *ptr = new car[t];
  20. > for(int a=0;a<t;a++,ptr++)
  21. >    {
  22. >        cin.get(); //to clear input queue(alt. between strings & 
  23. numbers).
  24. >        cout<<"Car #"<<a+1;
  25. >        cout<<"\nPlease enter make: ";     
  26. >        cin.getline(ptr->make,ArSize);
  27. >        cout<<"\nEnter year: ";
  28. >        cin>>ptr->year;
  29. >    } 
  30. >return;
  31. >}
  32.  
  33. You need to grab ahold of the pointer before you increment it 
  34. with another variable.
  35.  
  36. In addition to this, you need to decide what it is you have..
  37.  
  38. The code you show [ car *ptr = new char[t]  ] is allocating a 
  39. pointer to an array.If you increment the pointer, you should 
  40. dereference it before attempting to assign values to the data 
  41. members. Think of it like you would a char* ..
  42.  
  43. char* ptr = new char[t];    
  44. char* hold = ptr;    
  45.  
  46. for (int a=0; a<t; a++, ptr++ )
  47. {
  48.     if ( *ptr == 'a' )      
  49.     // do something    
  50. }
  51.  
  52. // now use the hold variable to iterate the array again
  53. for (a=0; a < t; a++)
  54.    cout << "letter " << a << " = " << hold[a] << '\n';
  55.  
  56. Although an easier way would be simply to access your new array 
  57. of car by index:
  58.  
  59.      cin.getline( ptr[a].make, ArSize );
  60.  
  61. If you actually want a pointer to an array of pointers, you need 
  62. to do it a bit differently:
  63.     
  64.     car **ptr = new car*[t];
  65.  
  66.  
  67. Lori.
  68.  
  69.